package org.akomantoso.core.data;

import java.io.IOException;
import java.util.Calendar;

import org.akomantoso.core.data.attribute.Date;
import org.akomantoso.core.data.group.ANtitleInline;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.OutputUtil;
import org.akomantoso.core.io.util.DateUtil;
import org.akomantoso.core.visitor.AKVisitable;
import org.akomantoso.core.visitor.AKVisitor;

/**
 * The element docDate is an inline element within preface to identify the string used by the document for its own
 * date(s). Documents with multiple dates may use multiple docDate elements.
 * 
 * <pre>
 * 	<xsd:element name="docDate">
 * 		<xsd:complexType mixed="true">
 * 			<xsd:complexContent>
 * 				<xsd:extension base="inline">
 * 					<xsd:attributeGroup ref="date"/>
 * 				</xsd:extension>
 *			</xsd:complexContent>
 * 		</xsd:complexType>
 * 	</xsd:element>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 * @since http://www.akomantoso.org/1.0
 * 
 */
public final class DocDate extends InlineType implements org.akomantoso.core.data.attribute.Date , ANtitleInline, AKVisitable {

	public static final String ELEMENT = "docDate";
	
	// Mandatory
	private Calendar _date;

	/**
	 * @see org.akomantoso.core.data.attribute.Date#getDate()
	 */
	@Override
	public Calendar getDate() {
		return _date;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Date#setDate(java.util.Calendar)
	 */
	@Override
	public void setDate(Calendar date) {
		_date = date;
	}

	/**
	 * @see org.akomantoso.core.data.InlineType#writeExternal(org.akomantoso.core.io.Output)
	 */
	@Override
	public <E extends Output> void writeExternal(E out) throws IOException {
		out.writeStart(ELEMENT);
		OutputUtil.writeMandatoryAttribute(out, org.akomantoso.core.data.attribute.Date.ATTRIBUTE, _date, this);
		super.writeExternal(out);
		out.writeEnd();
	}

	/**
	 * @see org.akomantoso.core.data.InlineType#readExternal(org.akomantoso.core.io.Input)
	 */
	@Override
	public <E extends Input> void readExternal(E in) throws IOException {
		// Mandatory
		_date = DateUtil.convert(in.readAttribute(Date.ATTRIBUTE));
		super.readExternal(in);
	}

	/**
	 * @see org.akomantoso.core.visitor.AKVisitable#accept(org.akomantoso.core.visitor.AKVisitor)
	 */
	@Override
	public void accept(AKVisitor visitor) {
		if (visitor.visitBegin(this)) {
			internalAccept(visitor);
			visitor.visitEnd(this);
		}
	}
	
}